package cn.zwx.search.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.StrUtil;
import cn.zwx.search.annotation.Document;
import cn.zwx.search.common.enums.RetCode;
import cn.zwx.search.exceptioin.BizException;
import cn.zwx.search.model.es.ESProduct;
import cn.zwx.search.model.req.DelDocReq;
import cn.zwx.search.model.req.FindDocByIdReq;
import cn.zwx.search.service.ProductService;
import cn.zwx.search.utils.ElasticsearchUtil;
import com.alibaba.fastjson.JSON;
import lombok.Data;
import lombok.SneakyThrows;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.util.List;
import java.util.Objects;

/**
 * @description: 商品APi
 * @projectName:search-example
 * @see:cn.zwx.search.service.impl
 * @author:zhangwenxue
 * @createTime:2020/10/24 13:28
 * @version:1.0
 */
@Service
public class ProductServiceImpl implements ProductService {
    private static Logger logger= LoggerFactory.getLogger(ProductServiceImpl.class);

    @Autowired
    private ElasticsearchUtil elasticsearchUtil;

    @Override
    public void createIndex(List<ESProduct> productList) {
        try {
            if(CollectionUtil.isEmpty(productList)){
                return;
            }
            ESProduct esProduct = productList.get(0);
            //创建索引
            elasticsearchUtil.createIndexIfNotExist(esProduct.getClass());
            //新增信息
            elasticsearchUtil.batchSaveOrUpdate(productList);
        } catch (Exception e) {
            logger.error("创建商品索引失败:{}",e);
        }
    }

    /**
     * @description 删除文档
     * @return java.lang.String
     * @author zhangwenxue
     * @createTime 2020/10/23 15:55
     **/
    @Override
    public void delDoc(DelDocReq delDocReq) {
        String index = getIndexName(delDocReq.getClass());
        boolean indexExists = elasticsearchUtil.isIndexExists(index);
        if (indexExists){
            throw new BizException(RetCode.PARAM_ERROR,"未查询到文档信息");
        }
        try {
            elasticsearchUtil.deleteDoc(index,delDocReq.getProductId());
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public String  getIndexName(Class<? extends Object> clazz) {
        Document annotation = clazz.getDeclaredAnnotation(Document.class);
        if (Objects.isNull(annotation)){
            throw new BizException(RetCode.PARAM_ERROR,"为获取到删除文档索引");
        }
        String index = annotation.index();
        if (StrUtil.isBlank(index)){
            throw new BizException(RetCode.PARAM_ERROR,"为获取到删除文档索引");
        }
        return index;
    }

    @Override
    public void delIndex(String index) {
        boolean indexExists = elasticsearchUtil.isIndexExists(index);
        if (indexExists){
            elasticsearchUtil.delIndex(index);
        }
    }

    @SneakyThrows
    @Override
    public void updateDoc(List<ESProduct> proList) {
        ESProduct esProduct = proList.get(0);
        String index = getIndexName(esProduct.getClass());
        boolean indexExists = elasticsearchUtil.isIndexExists(index);
        if (!indexExists){
            throw new BizException(RetCode.PARAM_ERROR,"未查询到文档信息");
        }
        for (ESProduct product : proList) {
            elasticsearchUtil.updateDoc(index,product.getProductId(),product);
        }
    }

    @SneakyThrows
    @Override
    public ESProduct findDocById(FindDocByIdReq req) {
        String index = getIndexName(req.getClass());

        String query = elasticsearchUtil.queryById(index, req.getDocId());

        ESProduct product = JSON.parseObject(query, ESProduct.class);

        return product;
    }


}
